import { ref, computed } from "vue";
import { defineStore } from "pinia";

export const useUserStore = defineStore("user", () => {
  //初始化用户数据
  const userInfo = ref({
    username: "",
    password: "",
    role: "student",
  });

  //set

  const setUserInfo = (data: any) => {
    // userInfo.value = data
    //本地持久化化
    localStorage.setItem("userInfo", JSON.stringify(data));
  };

  //get

  const getUserInfo = () => {
    const userInfo = localStorage.getItem("userInfo");
    if (userInfo) {
      return JSON.parse(userInfo);
    }

    return null;
  };

  //清空值

  const clearUserInfo = () => {
    userInfo.value = {
      username: "",
      password: "",
      role: "student",
    };
    localStorage.removeItem("userInfo");
  };

  return {
    setUserInfo,
    getUserInfo,
    clearUserInfo,
  };
});
